package com.cda.pes.service.impl;

import com.cda.pes.mapper.AddressMapper;
import com.cda.pes.pojo.Address;
import com.cda.pes.service.IAddressService;
import com.cda.pes.service.IDistrictService;
import com.cda.pes.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

/** 新增收货地址的实现类*/
@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;
    //添加用户的收货地址的业务层,依赖于DistrictService的业务层接口
    @Autowired
    private IDistrictService districtService;

    @Value("${user.address.max-count}")
    private Integer maxCount;

    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        //调用收货地址统计的方法
        Integer count = addressMapper.countByUid(uid);
        if (count >= maxCount){
            throw new AddressCountLimitException("收货地址数量超出上限");
        }

        //对address对象中的数据进行补全:省市区

        String ProvinceName = districtService.getNameByCode(address.getProvinceCode());
        String CityName = districtService.getNameByCode(address.getCityCode());
        String AreaName = districtService.getNameByCode(address.getAreaCode());

        address.setProvinceName(ProvinceName);
        address.setCityName(CityName);
        address.setAreaName(AreaName);

        //uid、isDefault
        address.setUid(uid);
        Integer isDefault = count == 0 ? 1 : 0;
        address.setIsDefault(isDefault);
        //补全4项日志
        address.setCreatedUser(username) ;
        address.setModifiedUser(username) ;
        address.setCreatedTime(new Date());
        address.setModifiedTime(new Date( )) ;

        //插入收货地址的方法
        Integer rows = addressMapper.insert(address);

        if (rows != 1){
            throw new InsertException("插入用户的收货地址产生未知异常");
        }

    }

    @Override
    public List<Address> getByUid(Integer uid) {
        List<Address> list = addressMapper.findByUid(uid);
//        for (Address address : list){
////            address.setAid(null);
////            address.setUid(null);
////            address.setProvinceCode(null);
////            address.setCityCode(null);
////            address.setAreaCode(null);
////            address.setTel(null);
////            address.setIsDefault(null);
////            address.setCreatedTime(null);
////            address.setCreatedUser(null);
////            address.setModifiedTime(null);
////            address.setModifiedUser(null);
//        }
        return list;
    }

    @Transactional
    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result == null){
            throw new AddressNotFoundException();
        }
        //检测当前获取到的收货地址数据的归属
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法数据访问");
        }
        //先将所有的收货地址设置非默认
        Integer rows = addressMapper.updateNonDefault(uid);
        if (rows < 1){
            throw new UpdateException("更新数据产生未知异常");
        }
        //将用户选中的某条地址设置为默认收获地址
        rows = addressMapper.updateDefaultByAid(aid, username, new Date());
        if (rows != 1){
            throw new UpdateException("更新数据产生未知异常");
        }
    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result == null){
            throw new AddressNotFoundException("收货地址数据不存在");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法数据访问");
        }
        Integer rows = addressMapper.deleteByAid(aid);
        if (rows != 1){{
            throw new DeleteException("删除数据产生未知异常");
        }}
        Integer count = addressMapper.countByUid(uid) ;
        if (count == 0) {
            //直接终止程序
            return;
        }

        if (result.getIsDefault() == 0){
            return;
        }
        //将这条数据中的is_ default 字符的值设置1
        Address address = addressMapper.findLastModified(uid);
        rows = addressMapper.updateDefaultByAid(
                address.getAid(),username,new Date());
        if (rows != 1){
            throw new UpdateException("更新数据时产生未知的异常");
        }


    }

    @Override
    public Address getByAid(Integer aid, Integer uid) {
        Address address = addressMapper.findByAid(aid);
        if (address == null){
            throw new AddressNotFoundException("收货地址数据不存在");
        }
        if (!address.getUid().equals(uid)){
            throw new AccessDeniedException("非法数据访问");
        }
        address.setCityCode(null);
        address.setProvinceCode(null);
        address.setAreaCode(null);
        address.setCreatedUser(null);
        address.setCreatedTime(null) ;
        address.setModifiedUser(null) ;
        address.setModifiedTime (null) ;
        return address;
    }


}
